package com.tangkeyon.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

/**
 * @Author: tangkeyon@gmail.com
 * @Date: 2019/6/1 21:18
 * @Version 1.0
 */
public class DateTimeUtil {

    public static  final String STANDARD_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";

    /**
     * @Description: 将时间对象转换为时间字符串
     * @param date
     * @param dateFormate
     * @return
     */
    public static String dateToStr(Date date, String dateFormate){
        if(date==null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime=new DateTime(date);
        return dateTime.toString(dateFormate);
    }

    /**
     * @Description: 将时间字符串转化为时间对象
     * @param dateStr
     * @param dateFormate
     * @return
     */
    public static  Date  strToDate(String dateStr,String dateFormate){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(dateFormate);
        return dateTimeFormatter.parseDateTime(dateStr).toDate();
    }

    public static String dateToStr(Date date){
        if(date==null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime=new DateTime(date);
        return dateTime.toString(STANDARD_DATE_FORMAT);
    }

    public static  Date  strToDate(String dateStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_DATE_FORMAT);
        return dateTimeFormatter.parseDateTime(dateStr).toDate();
    }
}
